package com.haohan.jingyuan.customer.service;

import com.haohan.jingyuan.common.enums.ErrorEnum;
import com.haohan.jingyuan.common.exception.BusinessException;
import com.haohan.jingyuan.common.util.EntityUtil;
import com.haohan.jingyuan.customer.dao.CustomerInfoMapper;
import com.haohan.jingyuan.customer.dao.CustomerTraceMapper;
import com.haohan.jingyuan.customer.dto.req.CreateTraceRequest;
import com.haohan.jingyuan.customer.dto.req.DeleteTraceRequest;
import com.haohan.jingyuan.customer.dto.req.QueryTraceListRequest;
import com.haohan.jingyuan.customer.dto.resp.TraceInfoResp;
import com.haohan.jingyuan.customer.dto.resp.TraceListResp;
import com.haohan.jingyuan.customer.entity.CustomerInfo;
import com.haohan.jingyuan.customer.entity.CustomerTrace;
import com.haohan.jingyuan.customer.helper.TraceRespHelper;
import com.haohan.jingyuan.gateway.holder.UserContextHolder;
import com.haohan.jingyuan.util.UuidUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 客户追踪服务实现类
 *
 * @ClassName CustomerTraceService
 * @Description 客户追踪相关业务逻辑实现
 * @Author hexijun
 * @Time 2025/01/19 15:00
 * @Version 1.0
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class CustomerTraceService {

    /**
     * 客户信息Mapper
     */
    private final CustomerInfoMapper customerInfoMapper;

    /**
     * 客户追踪Mapper
     */
    private final CustomerTraceMapper customerTraceMapper;

    /**
     * 新增追踪
     *
     * @param request 新增追踪请求
     * @return 追踪信息
     */
    public TraceInfoResp createTrace(CreateTraceRequest request) {
        // 验证客户是否存在
        CustomerInfo customerInfo = customerInfoMapper.findByCustomerId(request.getCustomerId());
        if (customerInfo == null) {
            throw new BusinessException(ErrorEnum.CUSTOMER_NOT_FOUND);
        }

        // 创建追踪信息
        CustomerTrace customerTrace = new CustomerTrace();
        customerTrace.setTraceId(UuidUtil.generateId("TRACE_", 10));
        customerTrace.setCustomerId(request.getCustomerId());
        customerTrace.setContent(request.getContent());
        EntityUtil.setCreateAndModifyInfo(customerTrace, UserContextHolder.getCurrentUserName());

        // 保存到数据库
        int result = customerTraceMapper.insert(customerTrace);
        if (result <= 0) {
            throw new BusinessException(ErrorEnum.DATABASE_ERROR);
        }
        TraceInfoResp traceInfoResp = TraceRespHelper.convert2TraceInfoResp(customerTrace);
        return traceInfoResp;
    }

    /**
     * 查询追踪列表
     *
     * @param request 查询追踪列表请求
     * @return 追踪列表
     */
    public TraceListResp queryTraceList(QueryTraceListRequest request) {
        // 查询追踪列表
        List<CustomerTrace> customerTraceList = customerTraceMapper.selectByCustomerId(request.getCustomerId());

        // 转换为响应对象
        List<TraceInfoResp> traceRespList = TraceRespHelper.convert2TraceInfoRespList(customerTraceList);

        // 构建响应对象
        TraceListResp response = new TraceListResp();
        response.setTraceList(traceRespList);
        response.setCustomerId(request.getCustomerId());
        response.setTotal(traceRespList.size());
        return response;
    }

    /**
     * 删除追踪
     *
     * @param request 删除追踪请求
     * @return 删除结果
     */
    public boolean deleteTrace(DeleteTraceRequest request) {
        // 查询追踪记录是否存在
        Long id = Long.parseLong(request.getId());
        CustomerTrace customerTrace = customerTraceMapper.selectById(id);
        if (customerTrace == null) {
            throw new BusinessException(ErrorEnum.TRACE_NOT_FOUND);
        }

        // 逻辑删除
        customerTrace.setIsDeleted("Y");
        EntityUtil.setModifyInfo(customerTrace, UserContextHolder.getCurrentUserName());

        // 更新数据库
        int result = customerTraceMapper.updateById(customerTrace);
        if (result <= 0) {
            throw new BusinessException(ErrorEnum.DATABASE_ERROR);
        }
        return true;
    }
}
